package com.zscat.mallplus.center.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.center.BaseController;
import com.zscat.mallplus.center.entity.CenterCash;
import com.zscat.mallplus.center.service.ICenterCashService;
import com.zscat.mallplus.center.utils.StringUtils;
import com.zscat.mallplus.fenxiao.entity.FenxiaoMemberTotal;
import com.zscat.mallplus.fenxiao.service.IFenxiaoMemberTotalService;
import com.zscat.mallplus.util.UserUtils;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/center/cash")
public class CenterCashController extends BaseController{

	@Resource
	private ICenterCashService centerCashService;
	@Resource
	private IFenxiaoMemberTotalService fenxiaoMemberTotalService;

	//列表
	@GetMapping("list")
	public Object getSysStoreCashByPage(CenterCash entity,
			@RequestParam(value = "kw", required = false) String kw,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
			) {
		try {
			QueryWrapper<CenterCash> wrapper = new QueryWrapper<>(entity).orderByDesc("create_time");
			if (StringUtils.isNoneBlank(kw)) {
				kw = kw.replace(" ", "");
				kw = "%" + kw + "%";
				wrapper.like("phone", kw);
			}
	        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
	        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        if (ValidatorUtils.notEmpty(entity.getStartTime()) && ValidatorUtils.notEmpty(entity.getEndTime())) {
	        	wrapper.between("create_time", dateFormat2.parse(dateFormat1.format(entity.getStartTime()) + " 00:00:00"), dateFormat2.parse(dateFormat1.format(entity.getEndTime()) + " 23:59:59"));
	        }
			return new CommonResult().success(centerCashService.page(new Page<CenterCash>(pageNum, pageSize), wrapper));
		} catch (Exception e) {
			log.error("根据条件查询所有提现列表：%s", e.getMessage(), e);
		}
		return new CommonResult().failed();
	}

	//修改
	@PostMapping(value = "/update/{id}")
	public Object updateSysStoreCash(@RequestBody CenterCash entity) {
		try {
			entity.setUpdateBy(UserUtils.getCurrentMember().getNickName());
			entity.setUpdateTime(new Date());
			if(entity.getStatus() == 2) {
				CenterCash cash = centerCashService.getById(entity.getId());
				FenxiaoMemberTotal fenxiaoMemberTotal = fenxiaoMemberTotalService.getOne(new QueryWrapper<FenxiaoMemberTotal>().eq("id", cash.getUserId()));
				fenxiaoMemberTotal.setCurrentAmout(fenxiaoMemberTotal.getCurrentAmout().add(cash.getMoney()));
				fenxiaoMemberTotalService.updateById(fenxiaoMemberTotal);
			}
			if (centerCashService.updateById(entity)) {
				return new CommonResult().success();
			}
		} catch (Exception e) {
			log.error("更新提现状态：%s", e.getMessage(), e);
			return new CommonResult().failed(e.getMessage());
		}
		return new CommonResult().failed();
	}

	@GetMapping(value = "/delete/{id}")
	public Object deleteSysStoreCash(@PathVariable Long id) {
		try {
			if (ValidatorUtils.empty(id)) {
				return new CommonResult().paramFailed("提现ID");
			}
			if (centerCashService.removeById(id)) {
				return new CommonResult().success();
			}
		} catch (Exception e) {
			log.error("删除提现：%s", e.getMessage(), e);
			return new CommonResult().failed(e.getMessage());
		}
		return new CommonResult().failed();
	}
}
